package db

import (
	"errors"
	"gorm.io/gorm"
	"hr/api/dto"
)

type InviteManage struct {
	AppModel
	UserID            string `gorm:"size:64" json:"user_id"`
	RecommendID       string `gorm:"size:64" json:"recommend_id"`
	Job               string `gorm:"size:255" json:"job"`
	IsReward          string `gorm:"size:255" json:"is_reward"`
	CreateBy          string `gorm:"size:128" json:"createBy"` // 创建人
	UpdateBy          string `gorm:"size:128" json:"updateBy"` // 更新人
	Remark            string `gorm:"size:255" json:"remark"`   //备注
	Status            string `gorm:"size:64;" json:"status"`
	User              `gorm:"-" json:"user"`
	Recommend         User `gorm:"-" json:"recommend"`
	RecruitmentManage `gorm:"-" json:"work"`
}

func (m *InviteManage) TableName() string {
	return "invite_manage"
}

type InviteManagePageModel struct {
	Total    int64              `json:"total"`
	Result   []InviteManage `json:"result"`
	PageNo   int                `json:"pageNo"`
	PageSize int                `json:"pageSize"`
}

func GetInviteManagePaging(pagination dto.Pagination) (InviteManagePageModel, error) {
	result := InviteManagePageModel{
		Total:    0,
		Result:   make([]InviteManage, 0),
		PageNo:   pagination.PageNo,
		PageSize: pagination.PageSize,
	}
	offset := (pagination.PageNo - 1) * pagination.PageSize
	dbs := GetInstance().Model(&InviteManage{})

	if err := dbs.Offset(offset).Limit(pagination.PageSize).Find(&result.Result).Error; err != nil {
		return result, err
	}
	if err := dbs.Offset(-1).Limit(-1).Count(&result.Total).Error; err != nil {
		return result, err
	}

	for i, manage := range result.Result {
		u, _, _ := GetUsers(map[string]interface{}{
			"id": manage.UserID,
		})
		result.Result[i].User = u
		r, _, _ := GetUsers(map[string]interface{}{
			"id": manage.RecommendID,
		})
		result.Result[i].Recommend = r
		j, _, _ := GetRecruitment(map[string]interface{}{
			"id": manage.Job,
		})
		result.Result[i].RecruitmentManage = j
	}

	return result, nil
}

func GetMyInviteManagePaging(pagination dto.Pagination, userId string) (InviteManagePageModel, error) {
	result := InviteManagePageModel{
		Total:    0,
		Result:   make([]InviteManage, 0),
		PageNo:   pagination.PageNo,
		PageSize: pagination.PageSize,
	}
	offset := (pagination.PageNo - 1) * pagination.PageSize
	dbs := GetInstance().Where("user_id = ?", userId).Model(&InviteManage{})

	if err := dbs.Offset(offset).Limit(pagination.PageSize).Find(&result.Result).Error; err != nil {
		return result, err
	}
	if err := dbs.Offset(-1).Limit(-1).Count(&result.Total).Error; err != nil {
		return result, err
	}

	for i, manage := range result.Result {
		u, _, _ := GetUsers(map[string]interface{}{
			"id": manage.UserID,
		})
		result.Result[i].User = u
		r, _, _ := GetUsers(map[string]interface{}{
			"id": manage.RecommendID,
		})
		result.Result[i].Recommend = r
		j, _, _ := GetRecruitment(map[string]interface{}{
			"id": manage.Job,
		})
		result.Result[i].RecruitmentManage = j
	}

	return result, nil
}

func GetInviteManage(p map[string]interface{}) (InviteManage, error, string) {
	f := InviteManage{}
	err := GetInstance().Where(p).First(&f).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return f, errors.New("查无记录"), "0"
		}
		return f, errors.New(err.Error()), "1"
	}
	return f, nil, "2"
}

func CreateInviteManage(p dto.InviteRequest) (InviteManage, error) {
	d := InviteManage{
		UserID:      p.UserId,
		RecommendID: p.RecommendId,
		Job:         p.JobId,
		IsReward:    p.IsReward,
	}
	if err := GetInstance().Create(&d).Error; err != nil {
		return d, err
	}
	return d, nil
}

func UpdateInviteManage(id, status string) (InviteManage, error) {
	f := InviteManage{
		RecommendID: id,
	}
	err := GetInstance().Model(&f).Updates(InviteManage{
		IsReward: status,
	}).Updates(map[string]interface{}{
		"recommend_id": id,
	}).Scan(&f).Error
	if err != nil {
		return f, err
	}
	return f, nil
}
